package com.chinatelecom.ifree.business.service.order.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.chinatelecom.ifree.business.dao.order.OrderDao;
import com.chinatelecom.ifree.business.dao.order.SystemConfigDao;
import com.chinatelecom.ifree.business.domain.base.BoolObjResult;
import com.chinatelecom.ifree.business.domain.base.Constant;
import com.chinatelecom.ifree.business.domain.base.CountResult;
import com.chinatelecom.ifree.business.domain.base.OrderResult;
import com.chinatelecom.ifree.business.domain.order.OrderEntity;
import com.chinatelecom.ifree.business.service.order.OrderService;
import com.chinatelecom.ifree.business.util.BaseResult;
import com.chinatelecom.ifree.business.util.ValidateUtil;

@Service
public class OrderServiceImpl implements OrderService {
	private static transient Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
	private ReadWriteLock lock = new ReentrantReadWriteLock();
	@Resource
	private OrderDao orderDao;
	@Resource
	private SystemConfigDao systemConfigDao;

	@Override
	public OrderResult saveOrder(OrderEntity entity) {
		OrderResult result = new OrderResult();
		result.setResult(false);
		if (logger.isInfoEnabled())
			logger.info("saveOrder=======entity={}", entity);
		// 校验参数
		String mobile = entity.getMobile();
		String name = entity.getName();
		if (StringUtils.length(name) == 0 || StringUtils.length(name) > 20) {
			result.setCode(BaseResult.ORDER_MISS_PARAMETER_ERROR.getCode());
			result.setMsg(BaseResult.ORDER_MISS_PARAMETER_ERROR.getMsg());
			return result;
		}
		if (!ValidateUtil.isMobile(mobile)) {
			result.setCode(BaseResult.ORDER_MISS_PARAMETER_ERROR.getCode());
			result.setMsg(BaseResult.ORDER_MISS_PARAMETER_ERROR.getMsg());
			return result;
		}
		Lock writeLock = lock.writeLock();
		writeLock.lock();
		try {
			// 先查询手机号码是否已经预约过
			OrderEntity orderEntity = orderDao.getOrderByMobile(entity.getMobile());
			if (orderEntity == null) {// 可以预约
				OrderEntity order = orderDao.saveOrderInfo(entity);
				if (order != null) {
					result.setResult(true);
					result.setOrderEntity(order);
				} else {
					result.setCode(BaseResult.ORDER_OCCUPY_FCODE_ERROR.getCode());
					result.setMsg(BaseResult.ORDER_OCCUPY_FCODE_ERROR.getMsg());
				}
			} else {// 已预约
				result.setCode(BaseResult.ORDER_MOBILE_EXIST_ERROR.getCode());
				result.setMsg(BaseResult.ORDER_MOBILE_EXIST_ERROR.getMsg());
			}
		} catch (Exception e) {
			logger.error("getOrderByMobile", e);
			result.setCode(BaseResult.ORDER_OCCUPY_FCODE_ERROR.getCode());
			result.setMsg(BaseResult.ORDER_OCCUPY_FCODE_ERROR.getMsg());
		} finally {
			writeLock.unlock();
		}
		return result;
	}

	@Override
	public OrderResult getOrder(String mobile) {
		OrderResult result = new OrderResult();
		result.setResult(false);
		try {
			OrderEntity ordeEntity = orderDao.getOrderByMobile(mobile);
			if (ordeEntity != null) {
				result.setResult(true);
				result.setOrderEntity(ordeEntity);
			} else {
				result.setCode(BaseResult.ORDER_OCCUPY_EMPTY_ERROR.getCode());
				result.setMsg(BaseResult.ORDER_OCCUPY_EMPTY_ERROR.getMsg());
			}
		} catch (Exception e) {
			result.setCode(BaseResult.ORDER_QUERY_ORDER_ERROR.getCode());
			result.setMsg(BaseResult.ORDER_QUERY_ORDER_ERROR.getMsg());
		}
		return result;
	}

	@Override
	public CountResult getTotalOrderNum() {
		CountResult result = new CountResult();
		result.setResult(false);
		try {
			long total = orderDao.getTotalOrder();
			result.setResult(true);
			result.setFirst(total);
		} catch (Exception e) {
			logger.error("getTotalOrderNum", e);
			result.setCode(BaseResult.ORDER_QUERY_TOTAL_ERROR.getCode());
			result.setMsg(BaseResult.ORDER_QUERY_TOTAL_ERROR.getMsg());
		}
		return result;
	}

	@Override
	public BoolObjResult getOrderInfo(Date fromDate, Date toDate, int page) {
		BoolObjResult result = new BoolObjResult();
		result.setResult(false);
		long pageCount = 0;
		try {
			pageCount = Long.parseLong(systemConfigDao.getSystemConfigByKey(Constant.ORDER_PAGE_SIZE).getContent());
		} catch (Exception e) {
			pageCount = 5000;
		}
		try {
			int start = new Long(page * pageCount).intValue();
			int end = new Long((page + 1) * pageCount).intValue();
			List<OrderEntity> list = orderDao.getOrderInfo(fromDate, toDate, start, end);
			result.setResult(true);
			result.setObj(list);
		} catch (Exception e) {
			logger.error("getTotalOrderNum", e);
			result.setCode(BaseResult.REPORT_QUERY_QUERY_ERROR.getCode());
			result.setMsg(BaseResult.REPORT_QUERY_QUERY_ERROR.getMsg());
		}
		return result;
	}

	@Override
	public CountResult getTotalOrder(Date fromDate, Date toDate) {
		CountResult result = new CountResult();
		result.setResult(false);
		try {
			long total = orderDao.getTotalOrder(fromDate, toDate);
			result.setResult(true);
			result.setFirst(total);
		} catch (Exception e) {
			logger.error("getTotalOrder", e);
			result.setCode(BaseResult.REPORT_QUERY_SECTION_ERROR.getCode());
			result.setMsg(BaseResult.REPORT_QUERY_SECTION_ERROR.getMsg());
		}
		return result;
	}
}
